package pe.edu.pucp.cpnc.mm.bean;

import java.util.ArrayList;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Nature extends Object{
	private int resources;
	private String typeResource; 
	
	public Nature(){
		
	}
	
	public Nature(int id, String name, int type, int path, int sizeX, int sizeY, ArrayList<String> sprite, int resources, String typeResources){
		super(id, name, type, path, sizeX, sizeY, sprite);
		this.resources = resources;
		this.typeResource = typeResources;
	}

	public int getResources() {
		return resources;
	}

	public void setResources(int resources) {
		this.resources = resources;
	}
	
	public String getTypeResource() {
		return typeResource;
	}

	public void setTypeResource(String typeResource) {
		this.typeResource = typeResource;
	}
	
        @Override
	public int add(int posX, int posY, Object object, Map map, int playerId){
            return add(posX, posY, object, map);
        }
        
        @Override
	public int add(int posX, int posY, Object object, Map map){
		
		Nature n = (Nature)object;
		int sX = n.getSizeX(); //Width Limit
		int sY = n.getSizeY(); //Height Limit
		
		for(int i=posY ;i<posY + sY; i++){
			for(int j=posX;j<posX + sX;j++){
				if(!(i>=0 && i<map.getSizeY() && j>=0 && j<map.getSizeX())){
					System.out.println("El objeto esta fuera de los limites.");
					return -1;
				}
				if(!(n.getPath() == map.getTiles()[i][j].getTile().getPath())){
					System.out.println("El objeto no puede atravesar este tile.");
					return -1;
				}
				if(map.getTiles()[i][j].getOccupied()==1){
					System.out.println("El tile esta ocupado.");
					return -1;
				}
			}			
		}
		ObjectDetail objDetail = new ObjectDetail();
		
		objDetail.setObject(n);
		objDetail.setPosX(posX);
		objDetail.setPosY(posY);
		objDetail.setOrientation(0);
		
		map.getPlayers().get(0).getObjectDetails().add(objDetail);

		for(int i=posY;i<posY + sY;i++){ //Set tile Occupied
			for(int j=posX;j<sX + posX;j++){
				map.getTiles()[i][j].setOccupied(1);
                                map.getTiles()[i][j].setObjInside(objDetail);
			}
		}
                map.getTiles()[posY][posX].setIsRoot(1);
		return 0;		
	}

}
